define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/privatesea/index' + location.search,
                    add_url: 'business/privatesea/add',
                    edit_url: 'business/privatesea/edit',
                    del_url: 'business/privatesea/del',
                    info_url: 'business/privatesea/info',
                    multi_url: 'business/privatesea/multi',
                    import_url: 'business/privatesea/import',
                    recovery_url: 'business/privatesea/recovery',
                    table: 'business',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'source.name', title: __('Sourceid') },
                        { field: 'gender', title: __('Gender'), searchList: { "0": __('保密'), "1": __('男'), "2": __('女') }, formatter: Table.api.formatter.normal },
                        { field: 'deal', title: __('Deal'), searchList: { "0": __('未成交'), "1": __('已成交') }, formatter: Table.api.formatter.normal },
                        { field: 'admin.nickname', title: __('Adminid') },
                        { field: 'visit.createtime', title: __('最后一次回访时间'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false,formatter: Table.api.formatter.datetime },
                        { field: 'visit.content', title: __('回访内容'), operate: 'LIKE',formatter: Table.api.formatter.content },
                        {
                            field: 'operate', title: __('Operate'),
                            table: table, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            // 自定义按钮封装
                            buttons: [
                                {
                                    name: 'info',
                                    title: '客户详细',
                                    icon: 'fa fa-bars',
                                    classname: 'btn btn-xs btn-primary btn-dialog btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.info_url,
                                    extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 根据class名称，来修改按钮的窗口大小
            $(".btn-add").data('area', ['100%', '100%'])
            $(".btn-edit").data('area', ['100%', '100%'])

            // 回收的弹框代码
            $(document).on("click", ".btn-recovery", function () {
                Layer.confirm(__('是否确认回收？'), { icon: 3, title: __('Warning'), shadeClose: true }, function (index) {
                    var ids = Table.api.selectedids(table);
                    Backend.api.ajax(
                        { url: $.fn.bootstrapTable.defaults.extend.recovery_url + `?ids=${ids}` },
                        function () {
                            table.bootstrapTable('refresh');
                            Layer.close(index);
                        }
                    );
                }
                );
            })
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        //详细
        info: function () {
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

            Controller.api.bindevent();
        },
        table: {
            // 客户资料方法
            base: function () {
                Controller.api.bindevent();
            },
            // 回访列表
            visit: function () {
                Controller.api.bindevent();
            },
            // 申请记录
            receive: function () {
                Controller.api.bindevent();
            },

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
